package org.jvending.provisioning.portal.impl;

import org.jvending.provisioning.portal.BundleDescriptorConverter;
import org.jvending.provisioning.stocking.par.*;

import javax.provisioning.BundleDescriptor;
import javax.provisioning.Deliverable;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;
import java.util.List;
import java.util.Locale;

public class BundleDescriptorConverterImpl implements BundleDescriptorConverter {

    public ProvisioningArchive toProvisioningArchive(List<BundleDescriptor> bundleDescriptors, Locale locale, String contentDeliveryUri) {
        try {
            ObjectFactory factory = new ObjectFactory();
            ProvisioningArchive provisioningArchive = factory.createProvisioningArchive();

            for (BundleDescriptor bundleDescriptor : bundleDescriptors) {
                ClientBundle clientBundle = factory.createClientBundle();

                clientBundle.setBundleType(bundleDescriptor.getBundleType().toString());

                //Content File
                Deliverable contentFileDeliverable = bundleDescriptor.getContentFile();
                if (contentFileDeliverable != null) {
                    ContentFile contentFile = factory.createContentFile();
                    contentFile.setMimeType(contentFileDeliverable.getMimeType());
                    contentFile.setValue(contentDeliveryUri + "/AdapterController?bundleId=" + bundleDescriptor.getBundleID());
                    clientBundle.setContentFile(contentFile);
                }

                clientBundle.setContentId(bundleDescriptor.getContentID());

                //Descriptor File
                Deliverable deliverable = bundleDescriptor.getDescriptorFile();
                if (deliverable != null) {
                    DescriptorFile descriptorFile = factory.createDescriptorFile();
                    descriptorFile.setMimeType(deliverable.getMimeType());
                    descriptorFile.setValue(contentDeliveryUri + "/AdapterController?bundleId=" + bundleDescriptor.getBundleID());
                    clientBundle.setDescriptorFile(descriptorFile);
                }

//                clientBundle.setToolDescriptions();
//                clientBundle.setUserDescriptions();

                //Vendor Info
                VendorInfo vendorInfo = factory.createVendorInfo();

                VendorDescription vendorDescription = factory.createVendorDescription();
                if (locale != null) vendorDescription.setLocale(locale.toString());
                vendorDescription.setValue(bundleDescriptor.getVendorDescription(locale));
                vendorInfo.setVendorDescription(vendorDescription);

                VendorName vendorName = factory.createVendorName();
                if (locale != null) vendorName.setLocale(locale.toString());
                vendorName.setValue(bundleDescriptor.getVendor(locale));
                vendorInfo.setVendorName(vendorName);

                VendorUrl vendorUrl = factory.createVendorUrl();
                if (locale != null) vendorUrl.setLocale(locale.toString());
                vendorUrl.setValue(vendorDescription.getValue());
                vendorInfo.setVendorUrl(vendorUrl);
                clientBundle.setVendorInfo(vendorInfo);

                clientBundle.setVersion(bundleDescriptor.getVersion());

                //Copyright
                List<Copyright> copyrights = clientBundle.getCopyright();
                String copyright = bundleDescriptor.getCopyright(locale);
                if (copyright != null) {
                    Copyright cr = factory.createCopyright();
                    cr.setValue(copyright);
                    if (locale != null) cr.setLocale(locale.toString());
                    copyrights.add(cr);
                }

                List<Preview> previews = clientBundle.getPreview();
                List<Deliverable> previewDeliverables = bundleDescriptor.getPreviews(locale);
                for (Deliverable pd : previewDeliverables) {
                    Preview preview = factory.createPreview();

                }
                provisioningArchive.getClientBundle().add(clientBundle);
            }
            return provisioningArchive;
        } catch (JAXBException e) {
            e.printStackTrace();
            return null;
        }
    }

    public String toProvisioningArchiveString(List<BundleDescriptor> bundleDescriptors, Locale locale, String contentDeliveryUri) {
        ProvisioningArchive par = toProvisioningArchive(bundleDescriptors, locale, contentDeliveryUri);
        StringWriter writer = new StringWriter();
        try {
            JAXBContext jc = JAXBContext.newInstance("org.jvending.provisioning.stocking.par");
            Marshaller m = jc.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
            m.setProperty(Marshaller.JAXB_ENCODING, "ISO-8859-1");
            m.marshal(par, writer);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
        return writer.toString();
    }
}
